﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using ROMAP.Entities.Menu;
using ROMAP.Utilities;
using FlatRedBall.Input;
using FlatRedBall.Graphics;
using System.IO;

namespace ROMAP.Screens
{
    public class HiScoreScreen : Screen
    {
        private Sprite background;
        private MenuButton done;
        private MenuButton menu;

        private Text hiScore;
        private List<int> scores;
        private List<string> strings;
        private List<Text> texts;
        private int hiScoreNumber = -1;

        private string scoretext;
        #region Methods

        #region Constructor and Initialize

        public HiScoreScreen()
            : base("HiScoreScreen")
        {
            // Don't put initialization code here, do it in
            // the Initialize method below
            //   |   |   |   |   |   |   |   |   |   |   |
            //   |   |   |   |   |   |   |   |   |   |   |
            //   V   V   V   V   V   V   V   V   V   V   V
            //Initialize(true);
        }

        public override void Initialize(bool addToManagers)
        {
            if (addToManagers)
            {
                AddToManagers();
            }
        }

        public override void AddToManagers()
        {
            // Set the screen up here instead of in the Constructor to avoid
            // exceptions occurring during the constructor.
            FlatRedBallServices.IsWindowsCursorVisible = true;

            // Initialize the sprites
            // Initialize background sprite
            background = SpriteManager.AddSprite(@"Content/Menu/MenuBackground");
            background.PixelScale();
            this.mSprites.Add(background);

            done = new MenuButton("Global", @"Content/Menu/DoneButton", "", 'e', 0, -12);
            done.collision.Visible = false;
            menu = new MenuButton("Global", @"Content/Menu/MainMenuButton", "", 'e', 20, -12);
            menu.collision.Visible = false;

            scores = new List<int>();
            strings = new List<string>();
            texts = new List<Text>();

            SetupHiScores();
        }
        #endregion

        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            if (!firstTimeCalled)
            {
                if (hiScoreNumber != -1)
                {
                    CheckForText();
                }
                checkCollisions();
            }
        }

        public override void Destroy()
        {
            base.Destroy();
            done.Destroy();
            menu.Destroy();
            SpriteManager.RemoveSprite(background);
            foreach (Text t in texts)
            {
                TextManager.RemoveText(t);
            }

        }
        #endregion

        #region Private Methods
        private void checkCollisions()
        {
            // Mouse collision check
            // Is Mouse on any button?
            if (InputManager.Mouse.IsOn(done.collision))
            {
                done.Hover();
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    WriteScores();
                    MoveToScreen(typeof(MenuScreen).FullName);
                }
            }
            else
                done.Idle();

            if (InputManager.Mouse.IsOn(menu.collision))
            {
                menu.Hover();
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    MoveToScreen(typeof(MenuScreen).FullName);
                }
            }
            else
                menu.Idle();
        }

        private void SetupHiScores()
        {
            StringBuilder strBuild = new StringBuilder();

            StreamReader read = new StreamReader(@"../../../../ROMAPContent/Score/hiscore.txt", false);
            while (!read.EndOfStream && scores.Count < 10)
            {
                string[] line = read.ReadLine().Split(' ');
                int nextScore = Int32.Parse(line[1]);
                if (nextScore > GameManager.DeathCounter && hiScoreNumber < 0)
                {
                    scores.Add(GameManager.DeathCounter);
                    strings.Add("");
                    texts.Add(CreateText(strings.Count - 1));
                    hiScoreNumber = strings.Count - 1;
                    texts[hiScoreNumber].SetColor(1, 0, 0);

                    if (texts.Count == 10)
                    {
                        break;
                    }
                }

                scores.Add(Int32.Parse(line[1]));
                strings.Add(line[0]);
                texts.Add(CreateText(strings.Count - 1));
            }

            read.Close();
        }

        private Text CreateText(int index)
        {
            Text text;
            text = TextManager.AddText(strings[index] + " " + scores[index]);
            text.Scale = .7f;
            text.Spacing = .7f;
            text.Visible = true;
            text.X = -3;
            text.Y = 5f - (1.5f * index);
            text.SetColor(1, 1, 1);
            text.Z = 0.5f;
            return text;
        }

        private void CheckForText()
        {
            if (strings[hiScoreNumber].Length < 12)
            {
                if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.A))
                {
                    strings[hiScoreNumber] += "A";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.B))
                {
                    strings[hiScoreNumber] += "B";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.C))
                {
                    strings[hiScoreNumber] += "C";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    strings[hiScoreNumber] += "D";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.E))
                {
                    strings[hiScoreNumber] += "E";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    strings[hiScoreNumber] += "F";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.G))
                {
                    strings[hiScoreNumber] += "G";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.H))
                {
                    strings[hiScoreNumber] += "H";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.I))
                {
                    strings[hiScoreNumber] += "I";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.J))
                {
                    strings[hiScoreNumber] += "J";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.K))
                {
                    strings[hiScoreNumber] += "K";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.L))
                {
                    strings[hiScoreNumber] += "L";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.M))
                {
                    strings[hiScoreNumber] += "M";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.N))
                {
                    strings[hiScoreNumber] += "N";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.O))
                {
                    strings[hiScoreNumber] += "O";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.P))
                {
                    strings[hiScoreNumber] += "P";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.Q))
                {
                    strings[hiScoreNumber] += "Q";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.R))
                {
                    strings[hiScoreNumber] += "R";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.S))
                {
                    strings[hiScoreNumber] += "S";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.T))
                {
                    strings[hiScoreNumber] += "T";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.U))
                {
                    strings[hiScoreNumber] += "U";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.V))
                {
                    strings[hiScoreNumber] += "V";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.W))
                {
                    strings[hiScoreNumber] += "W";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.X))
                {
                    strings[hiScoreNumber] += "X";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.Y))
                {
                    strings[hiScoreNumber] += "Y";
                }
                else if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.Z))
                {
                    strings[hiScoreNumber] += "Z";
                }

                
            }

            if (InputManager.Keyboard.KeyTyped(Microsoft.Xna.Framework.Input.Keys.Back) && strings[hiScoreNumber].Length > 0)
            {
                int length = strings[hiScoreNumber].Length;

                strings[hiScoreNumber] = strings[hiScoreNumber].Substring(0, length - 1);
            }

            Console.Out.WriteLine(strings[hiScoreNumber]);
            texts[hiScoreNumber].DisplayText = strings[hiScoreNumber] + " " + scores[hiScoreNumber];

        }

        private void WriteScores()
        {
            StreamWriter write = new StreamWriter(@"../../../../ROMAPContent/Score/hiscore.txt", false);
            foreach (Text t in texts)
            {
                write.WriteLine(t.DisplayText);
            }
            write.Close();
        }

        #endregion

        #endregion
    }
}